package com.cloudbench.util;

import static com.cloudbench.common.Constants.TIME_05_MINUTE;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import ch.boye.httpclientandroidlib.HttpEntity;
import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.NameValuePair;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
import ch.boye.httpclientandroidlib.client.methods.HttpPost;
import ch.boye.httpclientandroidlib.entity.ByteArrayEntity;
import ch.boye.httpclientandroidlib.entity.StringEntity;
import ch.boye.httpclientandroidlib.message.BasicHeader;
import ch.boye.httpclientandroidlib.protocol.HTTP;
import ch.boye.httpclientandroidlib.util.EntityUtils;

public class WebClient {
	static final String CNAME = WebClient.class.getSimpleName();
	
	public static WebClient create() {
		return new WebClient();
	}
	
	/**
	 * Get
	 * @param url
	 * @return
	 */
	//public static String get(String url) throws IOException {		
	//	return get(url, CONNECTION_TIMEOUT, SOCKET_TIMEOUT);
	//}
		
	
	/**
	 * Get 
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @return
	 * @throws IOException
	 */
	public static String get(Context c, String url, String sid) throws Exception {		
		if(StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");
		
		HttpClient httpClient = WebClientFactory.createHttpClient(c);
		HttpGet httpGet = WebClientFactory.createHttpGet(url, sid);
	
		HttpResponse res = httpClient.execute(httpGet);
		HttpEntity entity = res.getEntity();
		
		//try {
			return EntityUtils.toString(entity, "UTF-8");
		//} finally {
		//	if(httpGet != null)
		//		httpGet.abort();
		//}
	}

	
	/*
	public static String get(String url, String sid, int readTimeout) throws IOException {
		if(StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");

		HttpClient client = WebClientFactory.createHttpClient(readTimeout);
		HttpGet get = createHttpGet(url, sid);
		HttpResponse response = client.execute(get);
		HttpEntity entity = response.getEntity();
			
		return EntityUtils.toString(entity, "UTF-8");
	} */
	
	/**
	 * Get 
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @param timeout
	 * @return
	 * @throws IOException
	 */
	public static String get(Context c, String url, String sid, int timeout) throws Exception {		
		if(StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");
		
		HttpClient client = WebClientFactory.createHttpClient(c, timeout);
		HttpGet get = WebClientFactory.createHttpGet(url, sid);
		HttpResponse res = client.execute(get);
		HttpEntity entity = res.getEntity();
			
		//try {
			return EntityUtils.toString(entity, "UTF-8");
		//} finally {
		//	if(get != null)
		//		get.abort();
		//}
	}
	
	
	/**
	 * Get InputStream
	 * @param c
	 * @param url
	 * @param sid
	 * @return
	 * @throws IOException
	 */
	public InputStream getStream(Context c, String url, String sid) throws IOException {
		try {
			return getStream(c, url, sid, TIME_05_MINUTE);
		} catch(Exception e) {
			
		}
		
		return null;
	}

	

	
	
	/**
	 * Get InputStream
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @param timeout
	 * @return
	 * @throws IOException
	 */
	public InputStream getStream(Context c, String url, String sid, int timeout) throws Exception {		
		if(c == null || StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");
		
		//HttpClient client = new DefaultHttpClient();
		HttpClient client = WebClientFactory.createHttpClient(c);
		HttpGet get = WebClientFactory.createHttpGet(url, sid);
		//HttpGet get = new HttpGet(url);
		//get.addHeader(KEY_HTTP_SESSION_HEADER, sid);	
		
		return client.execute(get).getEntity().getContent();
	}
	
			
	/**
	 * Post
	 * @param url
	 * @param sid
	 * @return
	 * @throws IOException
	 */
	public static String post(Context c, String url, String sid) throws Exception {
		if(StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");
		
		HttpClient client = WebClientFactory.createHttpClient(c);
		HttpResponse res = client.execute(WebClientFactory.createHttpPost(url, sid));
		HttpEntity entity = res.getEntity();
		
		return EntityUtils.toString(entity, "UTF-8");
	}
	
	/**
	 * Post 
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @param timeout
	 * @return
	 * @throws Exception
	 */
	public static String post(Context c, String url, String sid, int timeout) throws Exception {
		if(StringUtil.isEmpty(url) || StringUtil.isEmpty(sid) )
			throw new IOException("url or sid is null");
		
		HttpClient client = null;
		if(timeout ==0)
			client = WebClientFactory.createHttpClient(c);
		else
			client = WebClientFactory.createHttpClient(c, timeout);
		
		HttpResponse res = client.execute(WebClientFactory.createHttpPost(url, sid));
		HttpEntity entity = res.getEntity();
	
		return EntityUtils.toString(entity, "UTF-8");
	}
	
	/**
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @param params
	 * @return
	 * @throws IOException
	 */
	public static String post(Context c, String url, String sid, ArrayList<NameValuePair> params) throws Exception {
		return post(c, url, sid, params, 0);
	}
	
	/**
	 * for login
	 * @param c
	 * @param url
	 * @param params
	 * @param timeout
	 * @return
	 * @throws IOException
	 */
	public static String post(Context c, String url, ArrayList<NameValuePair> params, int timeout) throws Exception {
		if (StringUtil.isEmpty(url) || params == null || params.size() == 0)
			throw new IOException("url or params is null");
		
		HttpClient client = null;
		if(timeout ==0)
			client = WebClientFactory.createHttpClient(c);
		else
			client = WebClientFactory.createHttpClient(c, timeout);
		
		HttpResponse res = client.execute(WebClientFactory.createHttpPost(url, params));
		HttpEntity entity = res.getEntity();
	
		return EntityUtils.toString(entity, "UTF-8");
	}
	
	/**
	 * Post
	 * 
	 * @param c
	 * @param url
	 * @param sid
	 * @param params
	 * @param timeout
	 * @return
	 * @throws IOException
	 */
	public static String post(Context c, String url, String sid, ArrayList<NameValuePair> params, int timeout) throws Exception {
		if (StringUtil.isEmpty(url) || params == null || params.size() == 0)
			throw new IOException("url or params is null");
		
		HttpClient client = null;
		if(timeout ==0)
			client = WebClientFactory.createHttpClient(c);
		else
			client = WebClientFactory.createHttpClient(c, timeout);
		
		HttpResponse res = client.execute(WebClientFactory.createHttpPost(url, sid, params, true));
		HttpEntity entity = res.getEntity();
	
		return EntityUtils.toString(entity, "UTF-8");
	}

	public static String post(Context c, String url, String sid, JSONObject jObject, int timeout) throws Exception {
		if (StringUtil.isEmpty(url) || jObject == null)
			throw new IOException("url or params is null");
		
		HttpClient client = null;
		if(timeout ==0)
			client = WebClientFactory.createHttpClient(c);
		else
			client = WebClientFactory.createHttpClient(c, timeout);
		
		HttpPost post = WebClientFactory.createHttpPost(url, sid);
		// Text encoding(utf-8) for JSON Object
		ByteArrayEntity entry = new ByteArrayEntity(jObject.toString().getBytes("UTF8"));
		entry.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(entry);
		
		HttpResponse res = client.execute(post);
		HttpEntity entity = res.getEntity();
	
		return EntityUtils.toString(entity, "UTF-8");
	}

	public static String post(Context c, String url, String sid, JSONArray jArray, int timeout) throws Exception {
		if (StringUtil.isEmpty(url) || jArray == null)
			throw new IOException("url or params is null");
		
		HttpClient client = null;
		if(timeout ==0)
			client = WebClientFactory.createHttpClient(c);
		else
			client = WebClientFactory.createHttpClient(c, timeout);
		
		HttpPost post = WebClientFactory.createHttpPost(url, sid);
		// Text encoding(utf-8) for JSON Array
		ByteArrayEntity entry = new ByteArrayEntity(jArray.toString().getBytes("UTF8"));
        entry.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(entry);
		
		HttpResponse res = client.execute(post);
		HttpEntity entity = res.getEntity();
	
		return EntityUtils.toString(entity, "UTF-8");
	}
	
	
}